/**
 * 保存案件编号
 * @param data
 * @param callback
 */
function saveCaseNumberOffLine(data,callback){
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO CASE_NUMBER (" +
    			"CASE_NUMBER_ID,REGEX,ORGANID,CASEWORD," +                
	            "CASEYEAR,ONLINENUMBER,OFFLINENUMBER,STARTNUMBER,ENDNUMBER,DIGIT) " +
    			"VALUES (?,?,?," +
    			"?,?,?,?,?,?,?)",
    			[data.id==undefined ? '' : data.id  ,data.regEx==undefined ? '' : data.regEx  ,
				 data.organId==undefined ? '' : data.organId,
    			 data.caseWord==undefined ? '' : data.caseWord,data.caseYear==undefined ? '' : data.caseYear,
    			 data.onlineNumber==undefined ? '' : data.onlineNumber,data.offlineNumber==undefined ? '' : data.offlineNumber,data.startNumber==undefined ? '' : data.startNumber,
    			 data.endNumber==undefined ? '' : data.endNumber,data.digit==undefined ? '' : data.digit],
       function (tx, result) {
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
       },
	   function (tx, error) {
    	   message("案件编号保存失败");
	   });
    });
}

/**
 * 通过主键ID获取案件编号信息
 * @param organId 主键
 * @param callback 回调函数
 **/
function findCaseNumberByOrganIdOffLine(organId,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM CASE_NUMBER WHERE ORGANID = ?", [organId], 
		function (tx, result){ 
		    var len = result.rows.length;  
		    if(len == 0){
		    	return;
		    }
		    var data = setCaseNumberDataOffLine(result);
		    if(data ==null)
		    	return;
		    var dataN = data[0];
		    if(parseInt(dataN.startNumber) > parseInt(dataN.endNumber)){
		    	message("离线分配的案件编号已使用完毕，请重新登陆");
		    	return;
		    }
		    var startNumber = parseInt(dataN.startNumber) + parseInt(dataN.offlineNumber) + 1;
		    var caseNumber = dataN.regEx.replace("案件登记",dataN.caseWord).replace("年份",dataN.caseYear).replace("编",startNumber);
		    $("#caseNumber").val(caseNumber);
		    updateCaseNumberOffLine(organId);
    	    if(typeof(callback) == 'function'){
    	    	callback(data[0]);
    	    }
	    }, 
	    function(tx, ex) {
        });  
	});
}


/**
 * 通过主键ID获取案件编号信息
 * @param organId 主键
 * @param callback 回调函数
 **/
function findCaseNumberDataByOrganIdOffLine(organId,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM CASE_NUMBER WHERE ORGANID = ?", [organId], 
		function (tx, result){ 
		    var data = setCaseNumberDataOffLine(result);
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
	    }, 
	    function(tx, ex) {
        });  
	});
}

/**
 * 组装数据list
 * @param result
 * @returns {Array}
 */
function setCaseNumberDataOffLine(result){
	var len = result.rows.length;
	if(len == 0){
		return null;
	}
    var data = new Array();
	for (var i = 0; i < len; i++){
		var caseNumber = {};
		caseNumber.id = result.rows.item(i).CASE_NUMBER_ID;
		caseNumber.regEx = result.rows.item(i).REGEX;
		caseNumber.xzqh = result.rows.item(i).XZQH;
		caseNumber.xzqhId = result.rows.item(i).XZQHID;
		caseNumber.zfml = result.rows.item(i).ZFML;
		caseNumber.zfmlId = result.rows.item(i).ZFMLID;
		caseNumber.caseWord = result.rows.item(i).CASEWORD;
		caseNumber.caseYear = result.rows.item(i).CASEYEAR;
		caseNumber.onlineNumber = result.rows.item(i).ONLINENUMBER;
		caseNumber.offlineNumber = result.rows.item(i).OFFLINENUMBER;
		caseNumber.startNumber = result.rows.item(i).STARTNUMBER;
		caseNumber.endNumber = result.rows.item(i).ENDNUMBER; 
		caseNumber.digit = result.rows.item(i).DIGIT;
	    data.push(caseNumber);
    }
	return data;
}

/**
 * 修改案件编号
 * @param organId 机构ID
 **/
function updateCaseNumberOffLine(organId){
	db.transaction(function (tx) { 
		tx.executeSql('UPDATE CASE_NUMBER SET STARTNUMBER = STARTNUMBER + 1 WHERE ORGANID = ?', [organId], 
				function (tx, results){
					
				}, 
				function (tx, ex){
					message("案件编号修改失败");
				});
	});
}

/**
 * 初始化案件编号
 * @param db
 * @param callback
 **/
function initCaseNumber(){
	caseNumber(getCaseNumberData);
}

/**
 * 清空数据
 */
function caseNumber(callback,data){
	//findCaseNumberDataByOrganIdOffLine($.cookie("organId"),);
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM CASE_NUMBER', [], 
		function (tx, results){
			if (typeof(callback) == 'function'){
				callback(data);
			}
		}, 
		function (tx, ex){
			message("删除案件编号失败");
		}); 
	});
}

/**
 * 从服务器上获取案件编号
 **/
function getCaseNumberData(caseNumberData){
	if(caseNumberData == null){
		$.ajax({
			type : "post",
			url : getContextPath() + "/organCaseNumber/findByOrganId",
			dataType : "json",
			data : {organId:organId},
			error : function(request) {
				message("获取案件编号数据失败");
			},
			success : function(data) {
				saveCaseNumberOffLine(data);
			}
		});
	}
}

/**
 * 保存机构案件字
 * @param data
 * @param callback
 */
function saveOrganZfml(data,callback){
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO ORGANZFML (" +
    			"CASE_ORGAN_ZFML_ID,ORGANID,ORGANNAME,ZFMLID,ZFML) " +
    			"VALUES (?,?,?,?,?)",
    			[data.id==undefined ? '' : data.id  ,data.organId==undefined ? '' : data.organId  ,data.organName==undefined ? '' : data.organName  ,data.zfmlId ==undefined ? '' : data.zfmlId,
    			 data.zfml==undefined ? '' : data.zfml],
       function (tx, result) {
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
       },
	   function (tx, error) {
    	   message("机构案件字保存失败");
	   });
    });
}

/**
 * 通过主键ID获取案件编号信息
 * @param organId 主键
 * @param callback 回调函数
 **/
function findOrganZfmlByOrganIdOffLine(organId,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM ORGANZFML WHERE ORGANID = ?", [organId], 
		function (tx, result){ 
		    var data = setOrganZfmlDataOffLine(result);
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
    	    
    	    return data;
	    }, 
	    function(tx, ex) {
        });  
	});
}

/**
 * 组装数据list
 * @param result
 * @returns {Array}
 */
function setOrganZfmlDataOffLine(result){
	var len = result.rows.length;
	if(len == 0){
		return null;
	}
    var data = new Array();
	for (var i = 0; i < len; i++){
		var organZfml = {};
		organZfml.id = result.rows.item(i).CASE_ORGAN_ZFML_ID;
		organZfml.organId = result.rows.item(i).ORGANID;
		organZfml.organName = result.rows.item(i).ORGANNAME;
		organZfml.zfmlId = result.rows.item(i).ZFMLID;
		organZfml.zfml = result.rows.item(i).ZFML;
	    data.push(organZfml);
    }
	return data;
}

/**
 * 初始化机构执法门类
 * @param db
 * @param callback
 **/
function initOrganZfml(){
	organZfml(getOrganZfmlData);
}

/**
 * 初始化机构执法门类
 */
function organZfml(callback,data){
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM ORGANZFML', [], 
		function (tx, results){
			if (typeof(callback) == 'function'){
				$.each(data, function(i, item) {
					/**
					 * 添加到本地
					 **/
					callback(item);
				});
				findOrganZfmlByOrganIdOffLine(organId,initZfmlLi);
			}
		}, 
		function (tx, ex){
			message("删除机构执法门类失败");
		}); 
	});
}

/**
 * 从服务器上获取
 **/
function getOrganZfmlData(organZfmlData){
	if(organZfmlData==null){
		$.ajax({
			type : "post",
			url : getContextPath() + "/organZfml/findOrganZfmlByOrganId",
			dataType : "json",
			data : {organId:$.cookie("organId")},
			error : function(request) {
				message("获取机构案件字失败");
			},
			success : function(data) {
				$.each(data, function(i, item) {
					/**
					 * 添加到本地
					 **/
					saveOrganZfml(item);
				});
			}
		});
	}
}